package webservice;

import ejb.facades.forum.MessageFacadeLocal;
import entities.User;
import entities.forum.Message;
import java.util.List;
import javax.ejb.EJB;
import javax.inject.Inject;
import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObjectBuilder;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import sessions.IUserAccountSession;

/**
 * Trabalho PD - 2014/2015
 * @author a21210380 a21210392
 */
@WebService(name = "MailWebService", serviceName = "MailWebService")
public class MailWebService {
    
    @Inject private IUserAccountSession userAccountSession;
    @EJB private MessageFacadeLocal messageFacadeLocal;

    @WebMethod(operationName = "getEmails")
    public String getEmails() {
        User user = userAccountSession.getUser();
        if(user != null) {
            List<Message> findReceivedEmails = messageFacadeLocal.findReceivedEmails(user);
            if (!findReceivedEmails.isEmpty()) {
                JsonArrayBuilder createArrayBuilder = Json.createArrayBuilder();
                
                for (Message p : findReceivedEmails) {
                    JsonObjectBuilder add = Json.createObjectBuilder()
                            .add("id_message", p.getId())
                            .add("message", p.getMessage())
                            .add("topic", p.getTopic())
                            .add("id_receiving_user", p.getReceivingUser().getId())
                            .add("name_receiving_user", p.getReceivingUser().getName())
                            .add("id_sent_user", p.getSentUser().getId())
                            .add("name_sent_user", p.getSentUser().getName());

                    createArrayBuilder.add(add.build());
                }

                return Json.createObjectBuilder().add("emails", createArrayBuilder.build()).build().toString();
            }
        }
        return "{\"emails\":[]}";
    }
    
    @WebMethod(operationName = "getEmail")
    public String getEmail(@WebParam(name = "email_id") int email_id) {
        User user = userAccountSession.getUser();
        if(user != null) {
            Message p = messageFacadeLocal.findReceivedEmail(user, email_id);
            if(p != null) {
                return Json.createObjectBuilder()
                            .add("id_message", p.getId())
                            .add("message", p.getMessage())
                            .add("topic", p.getTopic())
                            .add("id_receiving_user", p.getReceivingUser().getId())
                            .add("name_receiving_user", p.getReceivingUser().getName())
                            .add("id_sent_user", p.getSentUser().getId())
                            .add("name_sent_user", p.getSentUser().getName())
                        .build().toString();
            }
        }
        return "{}";
    }
    
    @WebMethod(operationName = "sendEmail")
    public String sendEmail(@WebParam(name = "receiver_id") int receiver_id, @WebParam(name = "topic") String topic, @WebParam(name = "message") String message) {
        return "{\"success\":" + userAccountSession.sendEmail(receiver_id, topic, message) + "}";
    }
}
